package vistas;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Vector;

import entidades.Casillas_correo;

import javax.swing.*;

import rmi.InterfazRemota;

public class MDIChildCorreoNuevo extends JInternalFrame {
	private JLabel lblDe;
	private JLabel txtDe;
	private JLabel lblPara;
	private JComboBox cmbPara;
	
	private InterfazRemota objetoRemoto;
	private Vector<Casillas_correo> vectorCasillas;
	private int id_casilla;
	private Vector<String> vectorPara;
	
	private JLabel lblAsunto;
	private JTextField txtAsunto;
	private JLabel lblEstado;
	private JLabel txtEstado; 
	private JLabel lblPrioridad;
	private JComboBox cmbPrioridad;
	private String[] prioridades = {"Normal","Urgente","Informativo"};
	private JLabel lblFecha;
	private JTextField txtFecha;
	
	private JButton btnEnviar;
	private JButton btnGuardar;
	private JTextArea txtArea;

	private static final long serialVersionUID = 1L;
	private static final int SEPARACION = 24;
	static int inset = 0;
	private MDI mdi;

    public MDIChildCorreoNuevo(MDI mdi) {
        super(null,true,true,true,true);
        this.mdi = mdi;
        objetoRemoto = mdi.getObjetoRemoto();
        this.id_casilla = mdi.getId_casilla();
        
        inicializar();
        setLocation(SEPARACION*inset, SEPARACION*inset);
        ++inset;
    }
    
    private void inicializar() {
        setName("mdiChild");
        vectorPara = new Vector<String>();
        

        
        lblDe =  new JLabel("De: ");
        txtDe = new JLabel();
        
        try {
			
        	vectorCasillas = objetoRemoto.listarCasillas();
	        for (int i=0;i<vectorCasillas.size();i++){
	        	vectorPara.add(vectorCasillas.elementAt(i).getCasilla());
	        	if (vectorCasillas.elementAt(i).getId_casilla()==id_casilla){
	        		txtDe.setText(vectorCasillas.elementAt(i).getCasilla());
	        	}
	        }

	        
	        
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        
        
        lblPara =  new JLabel("Para: ");
        cmbPara = new JComboBox(vectorPara);
               
        lblAsunto = new JLabel("Asunto");
        txtAsunto = new JTextField();
        lblEstado = new JLabel("Estado");
        txtEstado = new JLabel("Sin enviar");
        lblPrioridad = new JLabel("Prioridad");
        cmbPrioridad = new JComboBox(prioridades);
        lblFecha = new JLabel("Fecha");
        txtFecha = new JTextField();        
        
        txtArea = new JTextArea("",4,1);
        btnEnviar = new JButton("Enviar");
        btnEnviar.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					objetoRemoto.enviarCorreo(txtDe.getText(),cmbPara.getSelectedItem().toString() , txtAsunto.getText(), txtArea.getText(), txtFecha.getText(), "Enviado", cmbPrioridad.getSelectedItem().toString());
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
        	
        });
        btnGuardar = new JButton("Guardar");
        btnGuardar.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					objetoRemoto.enviarCorreo(txtDe.getText(), cmbPara.getSelectedItem().toString(), txtAsunto.getText(), txtArea.getText(), txtFecha.getText(), "Guardado", cmbPrioridad.getSelectedItem().toString());
					txtEstado.setText("Guardado");
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}			
			}
        	
        });
        
        

        

        
        
        
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,1));
        panel2.add(txtArea);
        
        Panel panel = new Panel();
        panel.setLayout(new GridLayout(7,2));
        panel.add(lblDe);
        panel.add(txtDe);
        panel.add(lblPara);
        panel.add(cmbPara);
        panel.add(lblAsunto);
        panel.add(txtAsunto);
        panel.add(lblEstado);
        panel.add(txtEstado);
        panel.add(lblPrioridad);
        panel.add(cmbPrioridad);
        panel.add(lblFecha);
        panel.add(txtFecha);
        
        panel.add(btnGuardar);
        panel.add(btnEnviar);
        
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(panel,BorderLayout.PAGE_START);
        container.add(panel2,BorderLayout.CENTER);
        
        int chWidth = (int) mdi.getWidth()*55/100;
        int chHeight = (int) mdi.getHeight()*50/100;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-chWidth)/2, (screenSize.height-chHeight)/2, chWidth, chHeight);
     }

}
